const BasicModel = require('./BasicModel');

module.exports = class extends BasicModel {
  async addAction(data) {
    const databack = await this.model('user_notice').addMany(data);
    return databack.length;
  }
  async readOffAction(params) {
    const readOff = {
      is_read: 1,
      read_time: params.data.read_time
    };
    const data = this.model(params.model).where(params.data).update(readOff);
    return data;
  }
  async readcountAction(params) {
    const type = params.data.type;
    let data = '';
    if (type === 'notice') {
      data = await this.model('notice').where({status: 1}).select();
      for (const i in data) {
        data[i].count = await this.where({notice_id: data[i].id, is_read: 1}).count();
      }
    } else {
      data = await this.model('announcement').where({status: 1}).select();
      for (const i in data) {
        data[i].count = await this.where({notice_id: data[i].id, is_read: 1}).count();
      }
    }
    return data;
  }
  // 阅读总数
  async readdetailAction(params) {
    const data = await this.model(params.model).where({notice_id: params.data.notice_id, is_read: 1}).select();
    const databack = [];
    for (const ele of data) {
      const num = await this.model('student_info').where({id: ele.user_id}).find();
      ele.userInfo = num;
      databack.push(think.omit(ele, 'id,notice_id,user_id,is_read,status'));
    }
    return databack;
    // 阅读详情：传参：notice_id:关联id,查找得到正在进行中的已读同学
  }
};
